<template>
	<view class="login-container" v-if="isShow">
		<view class="wechatapp">
			<view class="header">
				<image :src="logo" class="logo-img" mode="aspectFill"></image>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<button class="btn-normal" @tap="login">微信登录</button>
		</view>
		<view class="no-login-btn">
			<button class="btn-normal" @click="onNotLogin">暂不登录</button>
		</view>
	</view>
</template>

<script>
	import { gotopage } from '@/common/gotopage.js';
	export default {
		data() {
			return {
				isShow: true,
				logo:''
			};
		},
		onLoad() {
			this.getLogo();
		},
		methods: {
			login(){
				let self = this;
				plus.oauth.getServices(function(servies) {
					let s = servies[0]
					if (!s.authResult) {
						s.authorize(function(e) {
							uni.showLoading({
								title: '登录中',
								mask: true
							});
							self._post('user.useropen/login', {
								code: e.code,
								source: 'app'
							}, result => {
								// 记录token user_id
								uni.setStorageSync('token', result.data.token);
								uni.setStorageSync('user_id', result.data.user_id);
								uni.setStorageSync('classify', options.classify);
								// 获取登录前页面
								let url = uni.getStorageSync('currentPage');
								let pageOptions = uni.getStorageSync('currentPageOptions');
								if(Object.keys(pageOptions).length > 0){
									url += '?';
									for(let i in pageOptions){
										url += i + '=' + pageOptions[i] + '&';
									}
									url = url.substring(0, url.length - 1);
								}
								self.isShow = false;
								console.log('url='+url);
								gotopage(url);
							}, false, () => {
								uni.hideLoading();
							});
						}, function(e) {
							console.log('登陆认证失败!');
							uni.showModal({
								title: '认证失败1',
								content: JSON.stringify(e),
							});
						});
					} else {
						console.log('已经登陆认证');
					}
				}, function(e) {
					console.log("获取服务列表失败：" + JSON.stringify(e));
				})
			},
			onNotLogin:function(){
				gotopage('/pages/index/index');
			},
			getLogo:function(){
				let self = this;
				uni.showLoading({
					title: '正在加载'
				})
				self._post('user.useropen/logo', {}, result => {
					console.log(result);
					self.logo = result.data.logo;
				}, false, () => {
					uni.hideLoading();
				});
			},
		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}
	
	.logo-img{
		width: 190rpx;
		height: 190rpx;
	}
</style>